package com.risetechnology.krystalkitchen1;

import java.text.DateFormatSymbols;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.risetechnology.adapter.ScheduleListAdapter;
import com.risetechnology.common.Const;
import com.risetechnology.common.Shared;
import com.risetechnology.data.Schedule;
import com.risetechnology.master.NavigationActivity;

@SuppressLint({ "NewApi" })
public class ActivityDaySchedule extends NavigationActivity {

	private ListView listviewSchedules;
	public static List<Schedule> scheduleList;
	private static ScheduleListAdapter listAdapter;

	private String dayOfMonth;
	private String month;
	private String year;

	private Calendar currentDate;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.activity_day_schedule);

		// Set contents
		setContents();
	}

	private void setContents() {
		currentDate = Calendar.getInstance();

		final TextView textviewTitle = (TextView) findViewById(R.id.tv_date);
		textviewTitle.setTypeface(Shared.font);
		String dayOfWeek = getIntent().getStringExtra(Const.KEY_DAY_OF_WEEK);
		month = getIntent().getStringExtra(Const.KEY_MONTH);
		dayOfMonth = getIntent().getStringExtra(Const.KEY_DAY_OF_MONTH);
		year = getIntent().getStringExtra(Const.KEY_YEAR);
		String strDate = dayOfWeek + ", " + month + " " + dayOfMonth + ", " + year;
		textviewTitle.setText(strDate);

		currentDate.set(Integer.parseInt(year), monthNameToNumber(month), Integer.parseInt(dayOfMonth));

		ImageView ivNext = (ImageView) findViewById(R.id.iv_right);
		ivNext.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				currentDate.add(Calendar.DAY_OF_YEAR, 1);

				// Update title
				month = currentDate.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.US);
				year = Integer.toString(currentDate.get(Calendar.YEAR));
				dayOfMonth = Integer.toString(currentDate.get(Calendar.DAY_OF_MONTH));
				String dayOfWeek = currentDate.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.US);

				String strDate = dayOfWeek + ", " + month + " " + dayOfMonth + ", " + year;
				textviewTitle.setText(strDate);

				// Load events 
				getSchedules(Integer.toString(currentDate.get(Calendar.DAY_OF_MONTH)), 
						monthNumberToMonthName(currentDate.get(Calendar.MONTH)), 
						Integer.toString(currentDate.get(Calendar.YEAR)));
				
				updateAddScheduleTitle();
			}
		});

		ImageView ivPrevious = (ImageView) findViewById(R.id.iv_left);
		ivPrevious.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				currentDate.add(Calendar.DAY_OF_YEAR, -1);

				// Update title
				month = currentDate.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.US);
				year = Integer.toString(currentDate.get(Calendar.YEAR));
				dayOfMonth = Integer.toString(currentDate.get(Calendar.DAY_OF_MONTH));
				String dayOfWeek = currentDate.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.US);

				String strDate = dayOfWeek + ", " + month + " " + dayOfMonth + ", " + year;
				textviewTitle.setText(strDate);

				// Load events 
				getSchedules(Integer.toString(currentDate.get(Calendar.DAY_OF_MONTH)), 
						monthNumberToMonthName(currentDate.get(Calendar.MONTH)), 
						Integer.toString(currentDate.get(Calendar.YEAR)));
				
				updateAddScheduleTitle();
			}
		});

		LinearLayout llAddSchedule = (LinearLayout) findViewById(R.id.ll_add_schedule);
		llAddSchedule.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(ActivityDaySchedule.this.getParent(), ActivityOrganiseSchedule.class);
				intent.putExtra(Const.KEY_DAY_OF_MONTH, dayOfMonth);
				intent.putExtra(Const.KEY_MONTH, month);
				intent.putExtra(Const.KEY_YEAR, year);
				goNextHistory("Organise Schedule", intent);
			}
		});
		
		updateAddScheduleTitle();
		
		TextView textviewAddSchedule = (TextView) findViewById(R.id.tv_add_schedule);
		textviewAddSchedule.setTypeface(Shared.font);

		listviewSchedules = (ListView) findViewById(R.id.lv_schedules);
		scheduleList = new ArrayList<Schedule>();
		listAdapter = new ScheduleListAdapter(ActivityDaySchedule.this, R.layout.list_item_event, scheduleList);
		listviewSchedules.setAdapter(listAdapter);

		getSchedules(dayOfMonth, month, year);
	}

	private void updateAddScheduleTitle() {
		/*
		 * Remove "Add Schedule" if the day passed
		 */
		Calendar realDate = Calendar.getInstance();

		// Disable add schedule for passed date
		if (realDate.get(Calendar.YEAR) > currentDate.get(Calendar.YEAR)|| 
				(realDate.get(Calendar.YEAR) == currentDate.get(Calendar.YEAR) 
				&& realDate.get(Calendar.MONTH) > currentDate.get(Calendar.MONTH)) ||
				(realDate.get(Calendar.YEAR) == currentDate.get(Calendar.YEAR)
				&& realDate.get(Calendar.MONTH) == currentDate.get(Calendar.MONTH)
				&& realDate.get(Calendar.DAY_OF_MONTH) > currentDate.get(Calendar.DAY_OF_MONTH) )
				) {
			LinearLayout llAddSchedule = (LinearLayout) findViewById(R.id.ll_add_schedule);
			llAddSchedule.setVisibility(View.GONE);
		} else {
			LinearLayout llAddSchedule = (LinearLayout) findViewById(R.id.ll_add_schedule);
			llAddSchedule.setVisibility(View.VISIBLE);
		}
	}
	
	public static void getSchedules(String dayOfMonth, String month, String year) {
		if (scheduleList == null) {
			scheduleList = new ArrayList<Schedule>();
		}

		if (month.length() > 3) {
			month = month.substring(0, 3);
		}

		Shared.dbAdapter.getSchedule(dayOfMonth, month, year);
		listAdapter.notifyDataSetChanged();
	}

	@SuppressLint("DefaultLocale")
	private int monthNameToNumber(String monthName) {
		if ("january".contains(monthName.toLowerCase())) {
			return 0;
		} else if ("february".contains(monthName.toLowerCase())) {
			return 1;
		} else if ("march".contains(monthName.toLowerCase())) {
			return 2;
		} else if ("april".contains(monthName.toLowerCase())) {
			return 3;
		} else if ("may".contains(monthName.toLowerCase())) {
			return 4;
		} else if ("june".contains(monthName.toLowerCase())) {
			return 5;
		} else if ("july".contains(monthName.toLowerCase())) {
			return 6;
		} else if ("august".contains(monthName.toLowerCase())) {
			return 7;
		} else if ("september".contains(monthName.toLowerCase())) {
			return 8;
		} else if ("october".contains(monthName.toLowerCase())) {
			return 9;
		} else if ("november".contains(monthName.toLowerCase())) {
			return 10;
		} else if ("december".contains(monthName.toLowerCase())) {
			return 11;
		}
		return 0;
	}

	public String monthNumberToMonthName(int month) {
		return new DateFormatSymbols().getMonths()[month];
	}
	
}
